package com.handydata.antifraud.ws.dap;

import com.alibaba.fastjson.JSONObject;
import com.handydata.antifraud.annotation.Comment;
import com.handydata.antifraud.common.constant.DataSourceCode;
import com.handydata.antifraud.common.exception.WsInvokeException;
import com.handydata.antifraud.to.dap.DishonestReqTO;
import com.handydata.antifraud.to.dap.DishonestResTO;
import com.handydata.antifraud.util.DateUtil;
import com.handydata.antifraud.ws.WsInvocationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.Map.Entry;

/**
 * 驭道反欺诈信息查询
 */
@Slf4j
@Service("dishonestWsService")
@Comment("驭道")
public class DishonestWsServiceImpl extends DataPlatformWsService<DishonestReqTO, DishonestResTO> implements WsInvocationService<DishonestReqTO, DishonestResTO> {

	@Override
	public DishonestResTO query(DishonestReqTO req) throws WsInvokeException {
		log.info("驭道请求参数 {}", req);
		DishonestResTO resTO = new DishonestResTO();
		if (StringUtils.isEmpty(req.getDeviceIdCode())) {
			log.error("驭道反欺诈信息参数校验 {}", "deviceId为空");
			resTO.setResCode("1111");
			resTO.setResMsg("驭道反欺诈信息接口deviceId参数为空");
			return resTO;
		}
		this.dataType = DataSourceCode.DISHONEST.getCode();
		resTO = executeQuery(req);
		return resTO;
	}

	@Override
	protected String createQueryXml(DishonestReqTO req) {
		StringBuilder xml = new StringBuilder();
		xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
		xml.append("<subatm>");
		xml.append("<application>GwBiz.Req</application>");
		xml.append("<version>1.0.0</version>");
		xml.append("<sendTime>").append(DateUtil.now14()).append("</sendTime>");
		xml.append("<transCode>300617</transCode>");
		xml.append("<channelId>").append(encryInfo.getChannelId()).append("</channelId>");
		xml.append("<channelOrderId>").append(DateUtil.now17()).append("</channelOrderId>");
		xml.append("<deviceId>").append(req.getDeviceIdCode()).append("</deviceId>");
		xml.append("</subatm>");
		req.setQueryXml(xml.toString());
		return xml.toString();
	}

	@Override
	protected DishonestResTO parseResultAndSave(String resultXml, String orderId, Date createDate) {
		log.info("调用驭道反欺诈信息查询接口返回报文：{}", resultXml);
		DishonestResTO resTO = new DishonestResTO();
		try{
			//解析xml，保存本地DB
			JSONObject jasonObject = JSONObject.parseObject(resultXml);
			if (null != jasonObject) {

				if ("0000".equals(jasonObject.get("resCode"))) {
					JSONObject json = (JSONObject) jasonObject.get("resData");
					JSONObject check = (JSONObject) json.get("user_data_check_result");
					if ("1".equals(check.get("isfound")) && json.get("device_check_result") != null) {
						JSONObject device = (JSONObject) json.get("device_check_result");
						for (Entry<String, Object> entry : device.entrySet()) {
							if ("1".equals(entry.getValue())) {
								resTO.setHitResult("1");
								break;
							}
						}
						if (json.get("geo_check_result") != null) {
							JSONObject geo = (JSONObject) json.get("geo_check_result");
							for (Entry<String, Object> entry : geo.entrySet()) {
								if ("1".equals(entry.getValue())) {
									resTO.setHitResult("1");
									break;
								}
							}
						}
					}
				}
				resTO.setResCodeOfDataDishonest((String)jasonObject.get("resCode"));
				resTO.setResCode((String)jasonObject.get("resCode"));
				resTO.setResMsg((String)jasonObject.get("resMsg"));
				//resTO.setReqId(dataCallLogsReqBO.getId());
			} else {
				resTO.setResCode("1111");
				resTO.setResMsg("驭道反欺诈信息查询接口返回数据异常");
			}
	    } catch (Exception e) {
	    	log.info("驭道反欺诈信息查询接口报文解析异常:{}", e);
	    	resTO.setResCode("1111");
			resTO.setResMsg("驭道反欺诈信息查询接口返回报文解析异常");
	    }
		log.info("调用驭道反欺诈信息查询接口返回结果：{}", JSONObject.toJSONString(resTO));
		return resTO;
	}

	@SuppressWarnings("unchecked")
	@PostConstruct
	@Override
	public void register() {
		registerToWsServiceFactory(this, DishonestReqTO.class);
	}

}
